package com.kbbywyclient.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.RecommendScoreModule;
import com.kbbywyclient.presenter.RecommendScorePresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.RecommendScoreContract;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.util.LogUtil;
import com.kbbywyclient.util.QrCodeUtils;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomToolBar;
import com.scwang.smartrefresh.layout.util.DensityUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Description:
 * Created by FHW
 * on 2017/5/9
 */

public class RecommendScoreActivity extends BaseActivity implements RecommendScoreContract.View {

    @BindView(R.id.v_toolbar)
    CustomToolBar v_toolbar;

    @BindView(R.id.tv_score)
    TextView tv_score;

    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;
    @Inject
    RecommendScorePresenter recommendScorePresenter;

    private static SHARE_MEDIA platform = SHARE_MEDIA.SINA;

    String inviteShareUrl;

    private int type = 0;//0-新浪 1-短信 2-wechat 3-wechat朋友圈

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_recommend_score;
    }

    @OnClick({R.id.tv_sina, R.id.tv_qq, R.id.tv_wechat, R.id.tv_wechat_circle})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_wechat:
                type = 0;
                share(type);
                setScreenAction("分享", "微信好友分享");
                break;
            case R.id.tv_wechat_circle:
                type = 1;
                share(type);
                setScreenAction("分享", "微信朋友圈分享");
                break;
            case R.id.tv_qq:
                type = 2;
                share(type);
                setScreenAction("分享", "QQ分享");
                break;
            case R.id.tv_sina:
                type = 3;
                share(type);
                setScreenAction("分享", "新浪微博分享");
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("推荐给朋友");
    }

    @Override
    protected void initView() {
        v_toolbar.setLeftViewOnClickLinster(v -> finish());
    }

    @Override
    public void initData() {
        showLoadingDialog(getString(R.string.text_loading));
        int salesId=PreUtils.getUser(this).getSalesID();
        recommendScorePresenter.getInviteShareDes();
        recommendScorePresenter.getShareUrl(salesId);
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getRecommendScoreComponent(new RecommendScoreModule(this)).inject(this);
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadInviteShareDes (String des) {
        String s = des + "其他赚取方式";
        SpannableString spannable = new SpannableString(s);
        spannable.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);      //设置下划线
            }

            @Override
            public void onClick(View widget) {
                ActivitySkipUtil.skipAnotherActivity(RecommendScoreActivity.this, ScoreEarnActivity.class);
            }
        }, s.length() - 6, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_02D1EA)), s.length() - 6, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_score.setHighlightColor(Color.TRANSPARENT);
        tv_score.setText(spannable);
        tv_score.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    public void loadShareUrlSuccess(String shareUrl) {
        //生成二维码
        inviteShareUrl=shareUrl+"&kbb="+System.currentTimeMillis();
        Observable.from(new String[]{inviteShareUrl}).
                map(s -> {
                    int size= DensityUtil.dp2px(180);
                    return QrCodeUtils.generateQrCode(s,size,size);
                }).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Subscriber<Bitmap>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showShort("生成二维码失败");
                    }

                    @Override
                    public void onNext(Bitmap bitmap) {
                        ivQrCode.setImageBitmap(bitmap);
                    }
                });
    }

    public void share(int type) {
        switch (type) {
            case 0:
                platform = SHARE_MEDIA.WEIXIN;
                break;
            case 1:
                platform = SHARE_MEDIA.WEIXIN_CIRCLE;
                break;
            case 2:
                platform = SHARE_MEDIA.QQ;
                break;
            case 3:
                platform = SHARE_MEDIA.SINA;
                break;
        }

        UMWeb web = new UMWeb(inviteShareUrl);
        web.setTitle("卡贝贝 — 信贷经理的业绩翻倍神器！");//标题
        web.setThumb(new UMImage(this, R.mipmap.icon_share_img));  //缩略图
        web.setDescription("每天更新上万贷款需求用户，资质齐全，实时更新，首次可以先免费体验，还不快来抢客户！");//描述

        ShareAction shareAction = new ShareAction(this)
                .setPlatform(platform)
                .setCallback(umShareListener)
                .withMedia(web);
        if(!TextUtils.isEmpty(inviteShareUrl)){
            shareAction.share();
        }
    }

    UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            LogUtil.d("platform:" + platform);
            ToastUtil.showShort("分享成功啦");
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (t != null) {
                LogUtil.d("throw:" + t.getMessage());
            }
            ToastUtil.showShort("分享失败啦");
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            LogUtil.d("platform:" + platform);
            ToastUtil.showShort("分享取消");
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

}
